<?php
/**
 * 商品类目查询 jd.union.open.category.goods.get
 *
 * See: https://union.jd.com/openplatform/api/693
 * User: Ken.Zhang <kenphp@yeah.net>
 * Date: 2019-05-12
 */
class UnionOpenCategoryGoodsGetRequest {

    /**
     * 父类目id(一级父类目为0)
     */
    private $parentId;

    /**
     * 类目级别(类目级别 0，1，2 代表一、二、三级类目)
     */
    private $grade;

    /**
     * 请求参数
     */
    public $apiParams = [];

    public function setParentId($parent_id)
    {
        $this->parentId = $parent_id;
        $this->apiParams['req']['parentId'] = $parent_id;
    }

    public function getParentId()
    {
        return $this->parentId;
    }

    public function setGrade($grade)
    {
        $this->grade = $grade;
        $this->apiParams['req']['grade'] = $grade;
    }

    public function getGrade()
    {
        return $this->grade;
    }

    public function getApiMethodName()
    {
        return 'jd.union.open.category.goods.get';
    }

    /**
     * 解析结果
     *
     * @param   string      $response
     * @throws  Exception
     * @return  mixed
     */
    public function getResult($response)
    {
        $responseKey = str_replace('.', '_', $this->getApiMethodName()) . '_response';

        $response = @json_decode($response, true);
        if (!array_key_exists($responseKey, $response))
            $resp = $response['errorResponse'];
        else {
            $response[$responseKey]['result'] = json_decode($response[$responseKey]['result']);

            $resp = $response[$responseKey];
        }

        return $resp;
    }

}